package com.egao.common.module.company.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.Position;
import com.egao.common.module.company.entity.vo.PositionFormVO;
import com.egao.common.module.company.entity.vo.PositionListVO;
import com.egao.common.module.company.mapper.PositionMapper;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.company.service.PositionService;
import com.egao.common.module.company.utils.CompanyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职位信息(Recruit)表服务实现类
 *
 * @author dl
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Autowired
    private CompanyService companyService;

    @Override
    public List<PositionListVO> listPage(PageParam<PositionListVO> page) {
        //申请时间
        page = (PageParam<PositionListVO>) CompanyUtil.putApplyTime(page);
        if (page.get("userId") != null) {
            Company company = getCompanyByUserId((Integer) page.get("userId"));
            if (company != null) {
                page.put("companyId", company.getId());
            }
        }

        return baseMapper.listPage(page);
    }

    @Override
    public JsonResult savePosition(PositionFormVO positionFormVO) {
        if (positionFormVO == null) {
            throw new ParameterException("职位为空");
        }

        Company company = getCompanyByUserId(positionFormVO.getCreateBy());
        if (company == null) {
            throw new BusinessException("单位不存在");
        }

        positionFormVO.setCompanyId(company.getId());

        //复制职位vo到实体
        Position position = new Position();
        BeanUtil.copyProperties(positionFormVO, position);
        //待审核
        position.setState(CompanyConstants.SUCCESS);

        if (save(position)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    @Override
    public JsonResult updatePosition(PositionFormVO positionFormVO) {
        Position position = checkFormData(positionFormVO);
        BeanUtil.copyProperties(positionFormVO, position);

        if (updateById(position)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    @Override
    public JsonResult updatePositionState(PositionFormVO positionFormVO) {
        Position position = checkFormData(positionFormVO);

        if (CompanyConstants.SUCCESS.equals(position.getState())) {
            position.setState(CompanyConstants.FAILED);
        } else {
            position.setState(CompanyConstants.SUCCESS);
        }

        if (updateById(position)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    @Override
    public JsonResult detail(Integer id) {
        if (id == null) {
            throw new ParameterException("职位编号为空");
        }

        Position position = getById(id);
        if (position == null) {
            throw new BusinessException("职位不存在");
        }

        PositionFormVO positionFormVO = new PositionFormVO();
        BeanUtil.copyProperties(position, positionFormVO);

        return JsonResult.ok("查询成功").setData(positionFormVO);
    }

    @Override
    public JsonResult list(Integer loginUserId) {
        if (loginUserId == null) {
            throw new ParameterException("用户编号不存在");
        }

        Company company = getCompanyByUserId(loginUserId);
        if (company == null) {
            throw new BusinessException("单位不存在");
        }

        List<Position> list = getPositionByCompanyId(company.getId());
        return JsonResult.ok("查询成功").setData(list);
    }

    /**
     * 检验修改数据
     * @param positionFormVO 职位表单
     * @return Position 职位
     */
    private Position checkFormData(PositionFormVO positionFormVO) {
        if (positionFormVO == null) {
            throw new ParameterException("职位为空");
        }

        if (positionFormVO.getId() == null) {
            throw new ParameterException("职位编号为空");
        }

        Position position = getById(positionFormVO.getId());
        if (position == null) {
            throw new BusinessException("职位不存在");
        }
        return position;
    }

    /**
     * 根据用户编号查询单位编号
     * @param userId 用户编号
     * @return Company 单位
     */
    @Override
    public Company getCompanyByUserId(Integer userId) {
        QueryWrapper<Company> qr = new QueryWrapper<>();
        qr.eq("user_id", userId);
        List<Company> list = companyService.list(qr);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Position> getPositionByCompanyId(Integer companyId) {
        QueryWrapper<Position> qw = new QueryWrapper();
        qw.eq("company_id", companyId).eq("state", CompanyConstants.NO_FOUND_COMPANY);
        return baseMapper.selectList(qw);
    }

    @Override
    public List<Position> getPositionByIds(String ids) {
        QueryWrapper<Position> qw = new QueryWrapper();
        qw.in("id", ids.split(","));
        return baseMapper.selectList(qw);
    }

    @Override
    public List<Position> getPositionNumber() {
        return baseMapper.selectNumberList();
    }
}
